﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace CalculatorLogic.Algebraic_Math
{
    public class Zeros : IOperator
    {
        public Equation Apply(Equation equation)
        {
            List<double> collection = new List<double>();
            if (equation.Input[0].Contains("-"))
            {
                equation.Input[0] = equation.Input[0].Replace("-", "+-");
            }
            foreach (string entry in equation.Input[0].Split('+'))
            {
                string temp = entry.Replace('x', '1');
                if (temp.Contains("^"))
                {
                    temp = temp.Replace('^', ' ');
                    if (temp.Contains("*"))
                    {
                        temp = (double.Parse(temp.Split('*')[0]) *
                            Math.Pow(double.Parse(temp.Split(new char[2] { '*', ' ' })[1]),
                            double.Parse(temp.Split(new char[2] { '*', ' ' })[2]))).ToString();
                    }
                    else
                    {
                        temp = Math.Pow(double.Parse(temp.Split(' ')[0]), double.Parse(temp.Split(' ')[1])).ToString();
                    }
                }
                if (temp.Contains("*"))
                {
                    temp = (double.Parse(temp.Split('*')[0]) * double.Parse(temp.Split('*')[1])).ToString();
                }
                collection.Add(int.Parse(temp));
            }
            double[] values = quadradicFormula(collection);
            if (values[0] != Math.Round(values[0], 0) && values[1] != Math.Round(values[1], 0))
            {
                equation.Solved = "{}";
                return equation;
            }
            if (values[0].Equals(values[1]))
            {
                equation.Solved = "{"+values[0]+"}";
            }
            else
            {
                equation.Solved = "{" + values[0] + " , " + values[1] + "}";
            }
            return equation;
        }

        private double[] quadradicFormula(List<double> list)
        {
            return new double[2] { -((-(list[1]) + Math.Sqrt(Math.Pow(list[1], 2) - 4 * list[0] * list[2])) / (2 * list[0])), -((-(list[1]) - Math.Sqrt(Math.Pow(list[1], 2) - 4 * list[0] * list[2])) / (2 * list[0])) };
        }

        private List<double> factorNumbers(List<double> collection)
        {
            List<double> values = new List<double>();
            List<double> tempvals = new List<double>();
            double high = 0;
            foreach (double num in collection)
            {
                if (num > high)
                {
                    high = num;
                }
            }
            for (int i = 1; i < high; i++)
            {
                foreach (double num in collection)
                {
                    if ((double)(num / i) == Math.Ceiling(num / i))
                    {
                        tempvals.Add(num / i);
                    }
                }
                tempvals.Add(i);
                if (tempvals.Count == 4)
                {
                    values.Clear();
                    foreach (double val in tempvals)
                    {
                        values.Add(val);
                    }
                }
                tempvals.Clear();
            }
            return values;
        }
    }
}
